<template>
  <div class="amnage">
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
    >
      <!-- 用户的表单信息 -->
      <el-form
        ref="form"
        :rules="rules"
        :inline="true"
        :model="form"
        label-width="80px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input placeholder="请输入姓名" v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input placeholder="请输入年龄" v-model="form.age"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限" prop="authority">
          <el-select v-model="form.authority" placeholder="请选择权限">
            <el-option label="管理员" value="3"></el-option>
            <el-option label="党员" value="2"></el-option>
            <el-option label="志愿者" value="1"></el-option>
            <el-option label="群众" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker
            v-model="form.birth"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker
        ></el-form-item>
        <el-form-item label="地址" prop="addr">
          <el-input placeholder="请输入地址" v-model="form.addr"></el-input>
        </el-form-item>
      </el-form>
      <!-- <span>这是一段信息</span> -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <div class="manage-header">
      <el-button @click="handleAdd" type="primary">+ 新增</el-button>

      <!-- 搜索区域 -->
      <el-form :model="userForm" :inline="true"
        ><el-form-item
          ><el-input placeholder="请输入名称" v-model="userForm.name"></el-input
        ></el-form-item>
        <el-form-item
          ><el-button type="primary" @click="onSubmit"
            >查询</el-button
          ></el-form-item
        ></el-form
      >
    </div>
    <el-table stripe :data="tableData" style="width: 100%" height="90%">
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="sexLabel" label="性别">
        <template slot-scope="scope">
          <span>{{ scope.row.sex === 1 ? "男" : "女" }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="age" label="年龄" width="100px"> </el-table-column>
      <el-table-column prop="birth" label="出生日期"> </el-table-column>
      <el-table-column prop="addr" label="地址"></el-table-column>
      <el-table-column prop="addr" label="设置">
        <template slot-scope="scope">
          <el-button sizi="mini" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button sizi="mini" type="danger" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
      <!-- <el-table-column prop="age" label="权限"> </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <div class="b">
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        @current-change="handlePage"
      >
      </el-pagination>
    </div>
    <!-- </div> -->
  </div>
</template>

<script>
import { getUser, addUser, editUser, delUser } from "../api";
export default {
  name: "MyUser",
  data() {
    return {
      dialogVisible: false,
      // 男val=1女val=0
      // 权限群众0,志愿者1,党员2,管理员3
      form: {
        name: "", //姓名
        age: "", //年龄
        sex: "", //性别
        authority: "", //权限
        birth: "", //日期
        addr: "", //地址
      },
      userForm: {
        name: "",
      },
      tableData: [],
      modalType: 0, //0表示新增1表示编辑
      total: 0, //当前条数
      pageData: {
        page: 1,
        limit: 10,
      },
      rules: {
        name: [{ required: true, message: "请输入姓名" }],
        age: [{ required: true, message: "请输入年龄" }],
        sex: [{ required: true, message: "请选择性别" }],
        authority: [{ required: true, message: "请选择权限" }],
        birth: [{ required: true, message: "请选择出生日期" }],
        addr: [{ required: true, message: "请填写地址" }],
      },
    };
  },
  mounted() {
    // 获取列表数据
    this.getList();
  },
  methods: {
    // 提交用户表单
    submit() {
      this.$refs.form.validate((valid) => {
        // console.log(valid, "valid");
        if (valid) {
          if (this.modalType === 0) {
            addUser(this.form).then(() => {
              // 调用新增成功
              this.getList();
            });
          } else {
            editUser(this.form).then(() => {
              // 调用新增成功
              this.getList();
            });
          }

          // 进行后续对表单数据存储处理
          console.log(this.form, "form");

          // 拿到表单数据
          // 清空表单数据
          this.$refs.form.resetFields();
          this.dialogVisible = false;
          // 关闭弹窗
        }
        // else
      });
    },
    // 弹窗关闭的时候触发
    handleClose() {
      console.log(this.form);
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    cancel() {
      this.handleClose();
    },
    handleAdd() {
      this.modalType = 0;
      this.dialogVisible = true;
    },
    //获取列表数据
    getList() {
      getUser({ params: { ...this.userForm, ...this.pageData } }).then(
        ({ data }) => {
          console.log(data);
          this.tableData = data.list;
          this.total = data.count || 0;
        }
      );
    },
    // 选择页码的回调
    handlePage(val) {
      // console.log(val);
      this.pageData.page = val;
      this.getList();
    },
    // 列表搜索条件
    onSubmit() {
      this.getList();
    },
    handleEdit(row) {
      this.handleEdit = 1;
      this.dialogVisible = true;
      // 定当前行进行深拷贝
      this.form = JSON.parse(JSON.stringify(row));
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delUser({ id: row.id }).then(() => {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="less" scoped>
.amnage {
  height: 90%;
}
.b {
  text-align: right;
}
.manage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>